<?php
/**
 * Created by PhpStorm.
 * User: wufengjie
 * Date: 2017/6/27
 * Time: 下午10:16
 */

namespace app\need\controller;
require(VENDOR_PATH . 'topthink/think-image/src/Image.php');

use think\Image;

class M_common// extends \think\Controller
{
    function toJson($code, $data, $msg)
    {
        $json_array = ['code' => $code, 'data' => $data, 'msg' => $msg];
        $json = json_encode($json_array, 256 | 128);
        // 256=JSON_UNESCAPED_UNICODE; 128=JSON_PRETTY_PRINT;
        if (empty(input("pre"))) {
            if (empty(input("callback"))) {
                return $json;
            }
            return input("callback") . '(' . $json . ')';
        }
        return '<pre>' . $json;
    }

    function checkUnset($param, $list = [])
    {
        $newList = array_merge($list, ['pre', 'allKey', 'callback', '_']);
        $count = count($newList);
        for ($i = 0; $i < $count; $i++) {
            $key = $newList[$i];
            if (isset($param[$key])) {
                unset($param[$key]);
            }
        }
        return $param;
    }

    function printR($obj, $objName = '输出对象')
    {
        if (empty(input('pre')))
            return;

        if (gettype($obj) == 'object' || gettype($obj) == 'array') {
            $json = json_encode($obj, JSON_UNESCAPED_UNICODE);
            $obj = json_decode($json, true);
        }
        echo '<pre>' . $objName . ' === ';
        print_r($obj);
        echo '</pre>';
    }

    function print_r($obj, $objName = '输出对象')
    {
        if (gettype($obj) == 'object' || gettype($obj) == 'array') {
            $json = json_encode($obj, JSON_UNESCAPED_UNICODE);
            $obj = json_decode($json, true);
        }
        echo '<pre>' . $objName . ' === ';
        print_r($obj);
        echo '</pre>';
    }

    function to_json_decode($json, $isToArray = false)
    {
        $type = gettype($json);
        if (in_array($type, ['object', 'array'])) {
            $json = json_encode($json, JSON_UNESCAPED_UNICODE);
        } else if ($type == 'string') {
            if (strpos($json, '<pre>') !== false) {
                $json = str_replace('<pre>', '', $json);
            }

            if (strpos($json, '</pre>') !== false) {
                $json = str_replace('</pre>', '', $json);
            }
        }
        return json_decode($json, $isToArray);
    }

    function setServerUrl($HttpUrl)
    {
        $ServerUrl = str_replace('http:/', '/data/wwwroot', $HttpUrl);
        return $ServerUrl;
    }

    function setHttpUrl($ServerUrl)
    {
        $HttpUrl = str_replace('http:/', '/data/wwwroot', $ServerUrl);
        return $HttpUrl;
    }

    /* 缩略图相关常量定义 */
    //常量，标识缩略图等比例缩放类型   const THUMB_SCALING   = 1;
    //常量，标识缩略图缩放后填充类型   const THUMB_FILLED    = 2;
    //常量，标识缩略图居中裁剪类型     const THUMB_CENTER    = 3;
    //常量，标识缩略图左上角裁剪类型   const THUMB_NORTHWEST = 4;
    //常量，标识缩略图右下角裁剪类型   const THUMB_SOUTHEAST = 5;
    //常量，标识缩略图固定尺寸缩放类型 const THUMB_FIXED     = 6;
    //src要服务器上的绝对路径，reCreate是否重新生成
    function make_thumb($src = '', $W = 220, $H = 220, $type = 3, $ext = 'png', $quality = 8, $reCreate = false)
    {
        if (!file_exists($src))
            return '图片文件不存在，路径须是服务器绝对路径';
        $image = Image::open($src);

        $thumb_name = $src . '_' . $W . 'x' . $H . $ext;
        if (!file_exists($thumb_name) || $reCreate) {
            $image->thumb($W, $H, $type);
            $image->save($thumb_name, $ext, $quality);
        }
        return 'ran';   //运行了
    }

    //src要服务器上的绝对路径，reCreate是否重新生成
    function make220and160thumb($src = '', $type = 3, $ext = 'png', $quality = 8, $reCreate = false)
    {
        if (!file_exists($src))
            return '图片文件不存在，路径须服务器绝对路径';
        $image = Image::open($src);

        $thumb_name = $src . '_220x220.' . $ext;
        if (!file_exists($thumb_name) || $reCreate) {
            $image->thumb(220, 220, $type);
            $image->save($thumb_name, $ext, $quality);
        }

        $thumb_name = $src . '_160x160.' . $ext;
        if (!file_exists($thumb_name) || $reCreate) {
            $image->thumb(160, 160, $type);
            $image->save($thumb_name);
        }

        return 'ran';   //仅表示运行了
    }

    function explodeImgUrlsStrAndDelNull($strs, $str)
    {
        $array = explode('|', $strs);
        $count = count($array);
        for ($i = 0; $i < $count; $i++) {
            if ($array[$i] == '') {
                unset($array[$i]);
            } else {
                $array[$i] = $this->setFullImgUrl($array[$i]);
            }
        }
        return $array;
    }

    function setFullImgUrl($imgUrl)
    {
        if (empty($imgUrl))
            return 'http://public.igoyp.com/img/default_logo_144x.png';
        if (strpos($imgUrl, 'http://') === false && strpos($imgUrl, 'https://') === false) {
            $imgUrl = 'http://o2o.igoyp.com' . $imgUrl;
        }
        return $imgUrl;
    }

    function setFullLinkUrl($url)
    {
        if (empty($url))
            return '';
        $isHttp = strpos($url, 'http');
        $isHttps = strpos($url, 'https');
        if ($isHttp == false && $isHttps == false) {
            $url = 'http://o2o.igoyp.com' . $url;
        }
        return $url;
    }
}